package api

import (
	"api/basic/config"
	"api/basic/proto/consultation"
	"api/handler/request"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"path/filepath"
)

func Consultation(c *gin.Context) {
	var data request.ConsultationReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": err.Error(),
		})
		return
	}
	// 单文件
	file, _ := c.FormFile("file")
	log.Println(file.Filename)
	ext := filepath.Ext(file.Filename)
	if ext != ".jpg" && ext != ".pkg" {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "文件格式",
			"data": ext,
		})
		return
	}
	if file.Size >= 3*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "文件过大",
			"data": file.Size,
		})

		return
	}
	dst := "./basic/images/" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)

	c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))

	userId := c.GetUint("userId")

	r, err := config.Cs.PatientAdd(c, &consultation.PatientReq{
		UserId:  int32(userId),
		Name:    data.Name,
		Content: data.Content,
		Url:     dst,
		Type:    int32(data.Type),
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"msg":  "问诊人添加成功",
		"data": r,
	})
	log.Printf("Greeting: %s", r)
}
